import dva from 'dva';
import {routerRedux} from 'dva/router'
import {call, put} from 'dva/saga'
import {message} from 'antd'
import {reqwest_get,reqwest_url} from '../utils/Reqwest.js'
import * as ordersServices from '../services/orders.js'
// import axios from 'axios'

// const delay =timeout => new Promise(resolve => setTimeout(resolve, timeout))
let _data = [];
// let token = window.localStorage.getItem('token')



export default {
    namespace: 'orders',
    state: {
    	list: [],
    	loading: false,
    	table: [],
    	pagination: {}
    },
    reducers: {
	    setupOrder(state, { payload }) {
	    	_data = []

	    	const pagination = { ...state.pagination };
	        pagination.total = payload.data.total;
	        pagination.current = payload.payload.current * 1

	    	for(let i of payload.data.list) {
	    		_data.push({
	    			  orders:i.serial_number,
			          key:i.id,
			          ID:i.id,
			          name:i.name,
			          size:i.square,
			          timer:i.booking_date,
			          company_name:i.company_name,
                region:i.region_name,
                pictures_info: i.pictures_info,
                store:i.store_name,
                store_rate: i.store_rate,
			          mobile:i.tel,
			          address:i.address,
                status:i.workflow_state,
                estimated_total: i.estimated_total,
                money:{value:
                  i.estimated_total,
                  editable: false,
                  state:undefined
                },
			          measure_master:i.measure_master,
			          rebate:i.rebate==undefined?'2%':i.rebate*100+'%',
			          rebate_1:i.rebate==undefined?'2':i.rebate*100,
			          total:i.total,
			          owner_name:i.owner_name,
			          remark:i.remark,
			          province:i.province,
			          city:i.city,
			          area:i.area,
			          street:i.street,
			          strategy_result:i.strategy_result,
			          description:<p>地址:&ensp;{i.province}&ensp;{i.city}&ensp;{i.area}&ensp;{i.street}&ensp;</p>
	    		})
	    	}
	        return {
	        	list: payload.data,
	        	table: _data,
	        	loading: false,
	        	pagination: pagination
	        }

	    },
	    loading(state, { payload }) {
	    	return{loading: true}
	    },
	    editStore(state, { payload }) {
	    	console.log(payload)
	    	return{...state,table: payload.payload}
	    },
	    editSave(state, { payload }) {
	    	message.success('更新成功')
	    	return{...state,table: payload.payload.table , loading: false}
	    },
    },
    effects:{
    	*fetch ({payload},{put,call}) {
    		yield put({type: 'loading'})
    		const { data, headers } = yield call(ordersServices.fetch, { payload });
    		yield put({type: 'setupOrder', payload:{data , payload}})
    	},
    	*listEdit ({payload },{put,call}) {
    		// const data = yield call(ordersServices.test, { payload });
    		yield put({type: 'editStore', payload:{payload }})
    	},
    	*listSave ({payload },{put,call}) {
    		yield put({type: 'loading'})
    		const { data, headers } = yield call(ordersServices.listSave, { payload });
    		if(data.status ==='success'){
    			yield put({type: 'editSave', payload:{payload}})
    		}
    	},
    },
    subscriptions: {
    	setup({history, dispatch}) {
    		// console(10000)
    		return history.listen(({pathname}) => {
    			// console.log(pathname)
    			if(pathname === '/app/orders'){
    				// dispatch({ type: 'fetch'})
    			}
    		})
    	}
    }

};
